/**
 *
 * The MIT License
 *
 * Copyright (c) 2009 Noritaka Horio All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * @author Noritaka Horio 
 * @copyright Copyright (c) 2009 Noritaka Horio All rights reserved.
 * @license The MIT License
 * @version $Rev: 186 $ $Date: 2009-11-07 12:32:26 +0000 (Sat, 07 Nov 2009) $
 * @see http://mootools.net
 * 
 */

var Configuration = {

	initialize: function() {
		this.container = $("configuration");
		this.save  = $(this.container).getElement("a.save");
		this.cancel = $(this.container).getElement("a.cancel");
		this.db = new Clock.Database({
			onOpen: function(database){
				this.initializeTab();
				this.initializeStackPanel();
				this.initializeThemeList();
				this.initializeStaticTaskList();
				this.initializePeriodicalTaskList();
				this.initializeButton();
			}.bind(this)
		});
	},

	initializeTab: function() {
		var tab			= this.container.getElement("div.tab");
		var navigation	= tab.getElement("ul.navigation");
		var group		= tab.getElement("div.panelGroup");
	
		var tabNavigation	= navigation.getElements("li a");
		var panel			= group.getChildren("div.panel");
		this.tab = new Control.TabPanel(tabNavigation, panel);
	},

	initializeStackPanel: function() {
		var panel		= this.container.getElement("div.stackPanel");
		var navigation	= panel.getElements("ul.navigation li a");
		var group		= panel.getElements("div.panelGroup div.panel");
		this.stackPanel = new Control.StackPanel(navigation, group);
	},

	initializeThemeList: function() {
		var themes = $("theme").getElements("ul li a");
		themes.each(function(v,k) {
			v.addEvent("click", function(element,key){
				for (var i = 0; i < themes.length; i++) {
					var theme = themes[i];
					(key == i) ? theme.addClass("active") : theme.removeClass("active");
				}	
			}.bind(v,[v,k]));
		});
		this.loadConfiguration();
	},

	initializeStaticTaskList: function() {
		var container = $("static");
		this.staticTaskList = new Control.StaticTaskList(container);

		var addRowButton = container.getElement("a.add");
		addRowButton.addEvent("click", this.onAddStaticTaskRowClick.bind(this));

		var callback = this.onLoadStaticTaskComplate.bind(this);
		this.db.execute(Configuration.Queries.STATIC, callback, {"type": "static"});
	},


	initializePeriodicalTaskList: function() {
		var container = $("periodical");
		this.periodicalTaskList = new Control.PeriodicalTaskList(container);

		var addRowButton = container.getElement("a.add");
		addRowButton.addEvent("click", this.onAddPeriodicalTaskRowClick.bind(this));

		var callback = this.onLoadPeriodicalTaskComplate.bind(this);
		this.db.execute(Configuration.Queries.INTERVAL, callback, {"type": "periodical"});
	},

	initializeButton: function() {
		this.save.addEvent("click", function() {
			this.db.execute(Configuration.Queries.DELETETASK, this.onSave);
			this.db.execute(Configuration.Queries.DELETECONFIG, this.onSave);

			var theme = $("theme").getElement("ul li a.active");
			var data = {"type": "theme", "value": theme.getProperty("href").replace("#", "")};
			this.db.execute(Configuration.Queries.ADDCONFIG, this.onSave, data);

			var rows = this.staticTaskList.getRows();
			rows.each(function(r,k){
				var data = {"type": "static", "value": r.time, "plugin": r.plugin};
				this.db.execute(Configuration.Queries.ADDTASK, this.onSave, data);
			}.bind(this));


			var rows = this.periodicalTaskList.getRows();
			rows.each(function(r,k){
				var data = {"type": "periodical", "value": r.time, "plugin": r.plugin};
				this.db.execute(Configuration.Queries.ADDTASK, this.onSave, data);
			}.bind(this));

			nativeWindow.close();

		}.bind(this));

		this.cancel.addEvent("click", function(){
			nativeWindow.close();
		});
	},

	onSave: function(result, event) {
air.trace(result);
	},

	loadConfiguration: function() {
		var callback = this.onLoadConfigurationComplate.bind(this);
		this.db.execute(Configuration.Queries.CONFIGURATION, callback);
	},

	onLoadConfigurationComplate: function(result) {
		var configurations = {};
		if (result.data != null) {
			var numRows = result.data.length;
			for (var i = 0; i < numRows; i++) {
				var row = result.data[i];
				var parameter = {"id": row.id, "type": row.type, "value": row.value};
				configurations[row.type] = parameter;
			}
		}
		this.configurations = configurations;
		var theme = $("theme").getElement("ul li a." + configurations.theme.value);
		theme.addClass("active");
	},

	onAddStaticTaskRowClick: function() {
		this.staticTaskList.addRow({
			"time": "01:00",
			"plugin": "processing"
		});
	},

	onAddPeriodicalTaskRowClick: function() {
		this.periodicalTaskList.addRow({
			"time": "60",
			"plugin": "processing"
		});
	},

	onLoadStaticTaskComplate: function(result) {
		if (result.data != null) {
			var numRows = result.data.length;
			for (var i = 0; i < numRows; i++) {
				var row = result.data[i];
				this.staticTaskList.addRow({
					"time": row.value,
					"plugin": row.plugin
				});
			}
		}
	},

	onLoadPeriodicalTaskComplate: function(result) {
		if (result.data != null) {
			var numRows = result.data.length;
			for (var i = 0; i < numRows; i++) {
				var row = result.data[i];
				this.periodicalTaskList.addRow({
					"time": row.value,
					"plugin": row.plugin
				});
			}
		}
	}

}

Configuration.Queries = {
	"DELETECONFIG": "DELETE FROM configuration",
	"DELETETASK": "DELETE FROM task",
	"ADDCONFIG": "INSERT INTO configuration (type,value) VALUES (:type,:value)",
	"ADDTASK": "INSERT INTO task (type,value,plugin,active) values (:type,:value,:plugin,0)",
	"INTERVAL": "SELECT id,type,value,plugin,active,update_time FROM task where type = :type",
	"STATIC": "SELECT id,type,value,plugin,active,update_time FROM task where type = :type",
	"CONFIGURATION": "SELECT id,type,value FROM configuration"
};

window.addEvent("domready", Configuration.initialize.bind(Configuration));